/*
 * GenericDAOPuro.java
 *
 * Created on October 5, 2007, 9:54 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package petstore.persistence.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import petstore.model.BusinessObject;
import petstore.model.CodeData;
import petstore.persistence.IGenericDAO;

/**
 *
 * @author Tom Hayden
 */
public class GenericDAOPuro implements IGenericDAO {
    
    protected EntityManagerFactory emf;

    @PersistenceUnit
    public void setEntityManagerFactory(EntityManagerFactory emf) {
        this.emf = emf;
    }
    
    public Integer insert(BusinessObject businessObject) {
        EntityManager em = this.emf.createEntityManager();
        try {
             em.persist(businessObject);
             return businessObject.getId();
        }
        finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public Integer update(BusinessObject businessObject) {
        EntityManager em = this.emf.createEntityManager();
        try {
             em.merge(businessObject);
             return businessObject.getId();
        }
        finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void remove(BusinessObject businessObject) {
        EntityManager em = this.emf.createEntityManager();
        try {
             businessObject = em.merge(businessObject);
             em.remove(businessObject);
        }
        finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    public BusinessObject findById(Class clazz, Integer id) {
        EntityManager em = this.emf.createEntityManager();
        try {
             return (BusinessObject) em.find(clazz, id);
        }
        finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    public List findByDescription(Class clazz, String description) {
        EntityManager em = this.emf.createEntityManager();
        try {
             Query query = em.createQuery("FROM "+clazz.getSimpleName()+" AS bo " +
                                          "WHERE bo.description LIKE :description");
             query.setParameter("description", description);
             return query.getResultList();
        }
        finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    public List findByCriteria(String queryString) {
        EntityManager em = this.emf.createEntityManager();
        try {
             Query query = em.createQuery(queryString);
             return query.getResultList();
        }
        finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    public List findByCriteria(String queryString, Map params) {
        EntityManager em = this.emf.createEntityManager();
        try {
             Query query = em.createQuery(queryString);
             Iterator it = params.keySet().iterator();
             while (it.hasNext()) {
                 String key = (String)it.next();
                 query.setParameter(key, params.get(key));
             }             
             return query.getResultList();
        }
        finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List findByNamedQuery(String namedQuery) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List findByNamedQuery(String namedQuery, Map params) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List findAll(Class clazz) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List executeNativeQuery(String queryString) {
        EntityManager em = this.emf.createEntityManager();
        try {
             Query query = em.createNativeQuery(queryString);
             return query.getResultList();
        }
        finally {
            if (em != null) {
                em.close();
            }
        }
    }

}
